var cheerio=require("cheerio");
var fs=require("fs");
var dbUtil=require("./DbOperator");
var tools=require("./tools.js");
var util = require('util');
var crypto = require('crypto');
var myredis = require('../../lib/myredis.js');
var read= function () {
    fs.readFile("./instance/abc/logs/debug-page.html","UTF-8",function(err,data){
       if(err) {
           console.info(err);
           return false;
       }
        var  $=cheerio.load(data);
        //DealCategory($);
        DealCity($);
    })
};
var makePrimarySql=function(categoryId,cnName,enName){
    var sql="insert into xbq_category(categoryId,categoryName,enName) values('%s','%s','%s');";
    return util.format(sql,categoryId,cnName,enName);
};
var makeSecondCategorySql=function(categoryId,name,parentId){
    var sql="insert into xbq_second_category(categoryId,categoryName,parentCategoryId)values('%s','%s','%s');";
    return util.format(sql,categoryId,name,parentId);
};
var makeCitySql= function (enName,cnName) {
    var sql="insert into xbq_city(enName,cnName)values('%s','%s');";
    return util.format(sql,enName,cnName);
}

var getPrimaryCategoryEnName= function (url) {
    return tools.getLastTermBySplitter(url,"/")
};

var DealCategory=function($){
    var result=[];
    $('.category-nav .primary-category').each(function(i,el){
        var onCategory={};
        onCategory['id']=$(el).attr("data-key");
        onCategory['name']=$(el).find("a.name").text();
        onCategory['url']=$(el).find("a.name").attr('href');
        var primarysql=makePrimarySql(onCategory['id'],onCategory['name'],getPrimaryCategoryEnName(onCategory['url']));
        dbUtil.executeSql(primarysql);
        var secondCategoryList=[];
        $(el).find("div.secondary-category>a").each(function(i,ele){
            var id=$(ele).attr("data-key");
            if(id!==undefined){
                var secondCategroy={};
                secondCategroy['id']=$(ele).attr("data-key");
                secondCategroy['name']=$(ele).html();
                secondCategroy['url']=$(ele).attr("href");
                secondCategoryList.push(secondCategroy);
                var secondCategorySql=makeSecondCategorySql(secondCategroy['id'],secondCategroy['name'],onCategory['id']);
                dbUtil.executeSql(secondCategorySql);
            }
        });
        onCategory['secondCategory']=secondCategoryList;
        result.push(onCategory);
    });
};

var DealCity=function($){
    $('div.city-list div.group a').each(function(i,el){
        var cnName=$(el).text();
        var enName=tools.getCityPYName($(el).attr('href'));
        dbUtil.executeSql(makeCitySql(enName,cnName));
    });
};
//read();
var readCategoryFile=function(){
    var category=require("./logs/shanghaicategory.json");
    var secondCategory=category['secondCategory'];
    for(a in secondCategory){
        test(secondCategory[a]);
        //console.info(secondCategory[a]);
    }
    //process.exit(0);
};

var test=function(link){
    //var link="http://www.dianping.com/search/category/1/65/g259";
    var linkhash = crypto.createHash('md5').update(link).digest('hex');
    //console.info(linkhash);
    var settings=require("./settings.json"); //../neocrawler/instance/abc/
     this.redis_urlinfo_db;
    myredis.createClient(
        settings['url_info_redis_db'][0],
        settings['url_info_redis_db'][1],
        settings['url_info_redis_db'][2],
        'redis',
        function(err,cli){
            this.redis_urlinfo_db = cli;
            this.redis_urlinfo_db.hgetall(linkhash,function(err,value){
                if(value['status']==='crawled_failure') {
                    console.info(value['url']);
                }
                console.info(value['status']);
                //console.info(value);
            });
        });
}
var testRegex=function(){
    var link="http://www.dianping.com/shanghai";
  //  var link="http://www.dianping.com/shanghai/hotel/r7171c113523";
    var url_pattern  = "^http://www.dianping.com/(shenzhen)$|(shanghai)$";
    var patt = new RegExp(url_pattern);
    if(patt.test(link)){
        console.info(11);
    }
};


//testRegex();
//test();
readCategoryFile();
